<?php
namespace Tbf\Bundle\YntAuthBundle\Service;
use Tbf\Bundle\CallbackSerializerBundle\CallbackSerializer;
use Tbf\Bundle\SerializerBundle\Handler\ClassNameAwareHandlerInterface;
use Tbf\Bundle\SerializerBundle\Handler\NewFromArray\NewFromArrayInterface;
use Tbf\Bundle\SerializerBundle\Handler\ToArray\ToArrayInterface;
use Tbf\Bundle\SerializerBundle\Serializer;
use Tbf\Bundle\SerializerBundle\SerializerAware;

class TransactionSerializer extends SerializerAware implements ToArrayInterface,NewFromArrayInterface,ClassNameAwareHandlerInterface{
    protected $callback_serializer;
    protected $media_manager;
    public function __construct(CallbackSerializer $callback_serializer,MediaManager $media_manager){
        $this->callback_serializer = $callback_serializer;
        $this->media_manager = $media_manager;
    }
    public function newFromArray($class_name, $input,$group=null)
    {
        $obj = $this->serializer->newInstanceWithoutConstructor($class_name);
        /** @var \Tbf\Bundle\YntAuthBundle\Model\Transaction $obj */
        $obj->setMedia($this->media_manager->getMediaByMediaNameEn($input['media_name_en']));
        $obj->setLoginCallback($this->callback_serializer->fromString($input['login_callback']));
        unset($input['login_callback']);
        unset($input['media_name_en']);
        $this->serializer->loadFromArrayByProperty($obj,$input);
        return $obj;
    }

    public function toArray($obj,$group=null)
    {
        /** @var \Tbf\Bundle\YntAuthBundle\Model\Transaction $obj */
        $output = $this->serializer->toArrayByProperty($obj);
        $output['media_name_en'] = $obj->getMedia()->getNameEn();
        $output['login_callback'] = $this->serializer->toArray($obj->getLoginCallback());
        unset($output['media']);
        return $output;
    }

    /**
     * @return string
     */
    static function getSerializeClassName()
    {
        return '\Tbf\Bundle\YntAuthBundle\Model\Transaction';
    }

}